"""Nose AEP

This plugin add options to nose to work together with nose-gae to 
setup your GAE+AEP local testing environment.

Options can be configured in the `setup.cfg` under a [nosetests] block:

    [nosetests]
    with-aep=1
    aep-root=PATH\TO\your_app\common

Copyright (C) 2009 Kevin Jiang

"""
import os
import sys

import nose.plugins

class NoseAEP(nose.plugins.Plugin):
    """Nose plugin extension

    To use with nose-gae to setup the django testing environment 
    with google appengine and app-engine-patch

    """
    enabled = False
    enableOpt = 'with_aep'
    name = 'aep'

    def options(self, parser, env=os.environ):
        parser.add_option("--with-%s" % self.name, dest='with_aep',
                          type="string", default="",
                          help="Setup GAE+AEP environment with app-engine-patch"
                          " environment settings [NOSE_WITH_AEP]")
        parser.add_option("--aep-root", dest="aep_root", 
                          type="string", default=None,
                          help="Set the path to the root directory of "
                          "app-engine-patch in your project")

    def configure(self, options, conf):
        self.conf = conf
        if hasattr(options, self.enableOpt):
            self.enabled = True

        if hasattr(options, "aep_root"):
            sys.path.insert(0, getattr(options, "aep_root"))

    def begin(self): 
        from appenginepatch.aecmd import setup_env
        setup_env(manage_py_env=True)

        from appenginepatch.appenginepatcher.patch import patch_all, setup_logging
        patch_all()

